//
//  RJHomeTopSectionView.m
//  AirQuality
//
//  Created by Po on 15/5/28.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJHomeTopSectionView.h"

@interface RJHomeTopSectionView ()

@property (retain, nonatomic) NSArray * titleArray;                     //标题数组
@property (retain, nonatomic) NSMutableArray * buttonArray;             //按钮数组
@property (assign, nonatomic) UIButton * currentButton;
@property (retain, nonatomic) UIView * bottomLine;                      //下方选择效果

@property (assign, nonatomic) CGFloat buttonWidth;                      //按钮宽度

@property (strong, nonatomic) UILabel * pmLable;


@end

@implementation RJHomeTopSectionView

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        _titleArray = [NSArray arrayWithArray:titles];
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    _buttonWidth = ScreenWidth / _titleArray.count;
    float a[7] = {_buttonWidth,_buttonWidth,_buttonWidth,_buttonWidth-5,_buttonWidth-8,_buttonWidth+1,_buttonWidth+7};
    CGFloat height = self.frame.size.height;
    _buttonArray = [NSMutableArray array];
    float buttonX = 0;
    for (NSUInteger i = 0; i < _titleArray.count ;i ++) {
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, 0, a[i], height)];
         buttonX += a[i];
        if ([_titleArray[i] isEqualToString:@"PM"]) {
           _pmLable  = [[UILabel alloc] init];
            _pmLable.text = @"2.5";
            _pmLable.frame = CGRectMake(_buttonWidth/2+12, height/3+2, 20, 20);
            _pmLable.font = [UIFont systemFontOfSize:9];
            [button addSubview:_pmLable];
        }
        [button setTitle:_titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:GetRGBColor(85, 85, 85, 1) forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:(DeviceType >= 6 ? 16 : 14)]];
        [button setTitleColor:SysMainColor forState:UIControlStateSelected];
        [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [_buttonArray addObject:button];
        if (i == 0) {
            [button setSelected:YES];
            _currentButton = button;
            [self addSubview:[self getBottomLineWithFrame:CGRectMake(0, self.frame.size.height - 3, _buttonWidth, 3)]];
        }
    }
}

- (UIView *)getBottomLineWithFrame:(CGRect)rect
{
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] initWithFrame:rect];
        [_bottomLine setBackgroundColor:SysMainColor];
    }
    return _bottomLine;
}

#pragma mark - event
- (void)pressButton:(UIButton *)sender
{
    if (_currentButton == sender) {
        return;
    }
    if ([sender.titleLabel.text isEqualToString:@"PM"]) {
        _pmLable.textColor = GetRGBColor(34, 187, 185, 1);
    }else{
        _pmLable.textColor = [UIColor blackColor];
    }
    NSInteger currentButtonCount = [_buttonArray indexOfObject:_currentButton];
    NSInteger senderCount = [_buttonArray indexOfObject:sender];
    
    //设置页面跳转
    BOOL isLeft = ((currentButtonCount > senderCount) ? YES: NO);
    if ([self selectChange:senderCount isLeft:isLeft]) {
        [_currentButton setSelected:NO];
        _currentButton = sender;
        [_currentButton setSelected:YES];
        
        //设置选中下标
        [UIView animateWithDuration:0.2 animations:^{
            float a[7] = {_buttonWidth-2,_buttonWidth,_buttonWidth,_buttonWidth-5,_buttonWidth-5-5,_buttonWidth+5+3,_buttonWidth+7};;
            CGRect rect = _bottomLine.frame;
            CGFloat width = 0;
            for (int i = 0; i < senderCount; i ++) {
                width += a[i];
            }
            rect.origin.x = width
            ;
            [_bottomLine setFrame:rect];
        }];
    }
}

#pragma mark - function
- (void)setSelectCount:(NSUInteger)count
{
    if (_currentButton) {
        [_currentButton setSelected:NO];
    }
    _currentButton = _buttonArray[count];
    [_currentButton setSelected:YES];
    [self selectChange:count isLeft:NO];
}

- (void)setSelectCount:(NSUInteger)count isLeft:(BOOL)isLeft
{
    if (_currentButton) {
        [_currentButton setSelected:NO];
    }
    _currentButton = _buttonArray[count];
    [_currentButton setSelected:YES];
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = _bottomLine.frame;
        rect.origin.x = count * _buttonWidth;
        [_bottomLine setFrame:rect];
    }];
    [self selectChange:count isLeft:isLeft];
}

/**选项变换时调用*/
- (BOOL)selectChange:(NSUInteger)count isLeft:(BOOL)isLeft
{
    if (_delegate) {
        return [_delegate HomeTopSectionView:self didSelectCount:count isLeft:isLeft];
    }
    return NO;
}

@end
